package org.victoriest.server.impl;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.victoriest.server.VictoriestServer;

public class EndrawingServer implements VictoriestServer {

    protected Logger logger = Logger.getLogger(getClass());
	
	protected InetSocketAddress address;
	protected int port;
	protected ServerBootstrap bootstrap;
	
	@Resource(name="pipeLineFactory")
	protected ChannelPipelineFactory pipelineFactory;
	
	@Override
	public int getPort() {
		return port;
	}

	@Override
	public void setPort(int port) {
		this.port = port;
	}
	
	public EndrawingServer(){
	}
	
	@PostConstruct
	public void init(){
		ChannelFactory channelFactory = new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());
		bootstrap = new ServerBootstrap(channelFactory);
		bootstrap.setPipelineFactory(pipelineFactory);
		
		address = new InetSocketAddress(port);
		
		bootstrap.bind(address);
		logger.info("------------EndrawingServer Started!-------------");
	}
	
}
